#!/usr/bin/env python
# $Id: test2-10.py df54dec975f4 2012/09/09 20:22:40 pgurumur $
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

__version__  = "$Revision: df54dec975f4 $"
__author__   = "$Author: pgurumur $"
__modified__ = "$Date: 2012-09-09 20:22:40Z $"

import optparse
import sys

from venya.lib import snmp
from venya.lib import cmdparser

class nagios(cmdparser.GetOpt):
   option_list = (
         optparse.make_option("-H", "--host", type = "str",
            help = "Hostname for the nagios check", dest = "host"),
         optparse.make_option("-c", "--community", type = "str",
            help = "SNMP v2c community string", dest = "community"),
         optparse.make_option("-m", "--module", type = "choice",
            help = "Nagios check to make [ 'memory', 'cpu', 'failover']",
            choices = ["memory", "cpu", "failover"], dest = "module"),
         optparse.make_option("-t", "--type", type = "str",
            help = "Type of device", dest = "devtype"),
         ) + cmdparser.GetOpt.option_list

   help = """

   Python script using pysnmp to check network devices"

   """

   def __init__(self, **Keywords):
      super(self.__class__, self).__init__(**Keywords)
      self._client = None
      self._type = None
      

   def checkmem(self):
      freemem = 0
      if self.client and self._type:
         if (self._type == "asa") or (self._type == "ios"):
            usedMemOid = "1.3.6.1.4.1.9.9.48.1.1.1.5.1"
            freeMemOid = "1.3.6.1.4.1.9.9.48.1.1.1.6.1"

            used = self._client.Get(usedMemOid)
            free = self._client.Get(freeMemOid)
            total = used + free
            freemem = (free / total) * 100
         elif self._type == "ive":
            freeMemOid = "1.3.6.1.4.1.12532.11.0"
            free = self._client.Get(freeMemOid)
            freemem = 100 - free

   def handle(self):
      (options, args) = self.parse()

      if options.host and options.community:
         self._client = snmp.snmp(server = options.host,
               community = options.community)

      if options.devtype:
         self._type = options.devtype.lower()


      if option.module.lower() == "memory":
         self.checkmem()
      elif options.module.lower() == "cpu":
         self.checkcpu()
      elif options.module.lower() == "failover":
         self.checkfailover()

if __name__ == "__main__":
   chknagios = None
   try:
      chknagios = nagios(version = 1.0, args = sys.argv)
      if len(chknagios) <= 1:
         chknagios.printhelp()
      else:
         chknagios.handle()
   except cmdparser.GetOptError, message:
      print message
      sys.exit(1)
      
